package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.inv.InvBillConfingDAO;
import light.head.dao.inv.InvBusinessDAO;
import light.head.dao.inv.InvDepotDAO;
import light.head.dao.inv.InvInStorageDAO;
import light.head.dao.inv.InvInStorageDetailDAO;
import light.head.dao.inv.InvInStorageItemDAO;
import light.head.dao.inv.InvReturnDetailDAO;
import light.head.dao.inv.InvReturnInfoDAO;
import light.head.dao.inv.InvReturnItemDAO;
import light.head.dao.inv.InvStorageDAO;
import light.head.dao.inv.InvStorageDetailDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvInStorage;
import light.head.model.inv.InvInStorageDetail;
import light.head.model.inv.InvInStorageItem;
import light.head.model.inv.InvReturnDetail;
import light.head.model.inv.InvReturnInfo;
import light.head.model.inv.InvReturnItem;
import light.head.model.inv.InvStorage;
import light.head.model.inv.InvStorageDetail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 销货退货
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_INVRETURNSALES)
public class InvReturnSalesController extends SupporterController{
	private InvReturnInfoDAO infoDAO;
	private InvReturnItemDAO itemDAO;
	private InvReturnDetailDAO detailDAO;
	private InvStorageDAO isDAO;
	private InvStorageDetailDAO isdDAO;
	private InvInStorageDAO iisDAO;
	private InvInStorageItemDAO iisiDAO;
	private InvInStorageDetailDAO iisdDAO;
	private OrganDAO organDAO;
	private InvBusinessDAO ibDAO;
	private InvBillConfingDAO ibcfDAO;
	private InvDepotDAO idDAO;
	  		
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request,HttpServletResponse response) throws Exception{
		return new ModelAndView(Url.INVRETURNSALES_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query (HttpServletRequest request, HttpServletResponse response) throws Exception{
		PageBean pb = createPageBean(request);
		Map<String,String> map = createQueryMapByParmName(request, Parm.DELETED,Parm.TIME1,Parm.TIME2,Parm.SNAME,Parm.CASHIER,Parm.DNAME,Parm.DN,Parm.CN);
		if (infoDAO.findByMap(Sys.B_TWO,organDAO.getSonCodeList(cu_organCode(request)), map, pb) == RESULT.SUCCESS) {
			InvReturnInfo isi =null;
			for (Object o : pb.getResultList()) {
				isi = (InvReturnInfo) o;
				isi.setItemList(itemDAO.findByRc(isi.getCode()));
			}
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.INVRETURNSALES_LIST);
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if (null != code) {
			InvReturnInfo is = infoDAO.get(Integer.parseInt(code));
			List<InvReturnItem> itemList = itemDAO.findByRc(is.getCode());
			is.setItemList(itemList);
			request.setAttribute(Parm.SALES, is);
		} 
		return new ModelAndView(Url.INVRETURNSALES_EDIT);
	}
	@RequestMapping(value = Url.DETAIL)
	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if (null != code) {
			InvReturnInfo iis = infoDAO.get(Integer.parseInt(code));
			List<InvReturnItem> itemList = itemDAO.findByRc(iis.getCode());
			iis.setItemList(itemList);
			for (InvReturnItem item : itemList) {
				List<InvReturnDetail> detailList = detailDAO.findByRic(item.getCode());
				item.setDetailList(detailList);
			}
			request.setAttribute(Parm.SALES, iis);
		} 
		return new ModelAndView(Url.INVRETURNSALES_DETAIL);
	}
	
	//销货退货
	@Transactional
	@RequestMapping(value = Url.SAVE)
	public void save(InvReturnInfo iri, HttpServletRequest request, HttpServletResponse response) throws Exception{
		RESULT result = RESULT.ERROR;
		List<InvReturnItem> itemList = iri.getItemList();			// 提交上来的数组
		iri.setDn(ibcfDAO.createBCode(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_ELEVEN));
		iri.setType(Sys.B_TWO);
		iri.setCt(new Date());
		iri.setDeleted(false);
		iri.setCu(cu_code(request));
		iri.setOc(cu_organCode(request));
		result = infoDAO.add(iri);
		//保存入库信息
		InvInStorage iis = new InvInStorage();
		iis.setDn(ibcfDAO.createBCode(organDAO.getRoot(cu_organCode(request)).getCode(), Sys.B_TEN));
		iis.setCt(new Date());
		iis.setCu(cu_code(request));
		iis.setDeleted(false);
		iis.setOc(cu_organCode(request));
		iis.setBc(iri.getBc());
		iis.setType(Sys.B_TWO);
		result = iisDAO.add(iis);
		// 保存
		for (InvReturnItem item : itemList) {
			if (item != null && item.getCc() != null && item.getCount() != null){
				// 保存退货明细
				item.setRc(iri.getCode());
				item.setDeleted(false);
				result = itemDAO.add(item);
				//保存入库明细
				InvInStorageItem iisi = new InvInStorageItem();
				iisi.setCc(item.getCc());
				iisi.setDc(item.getDc());
				iisi.setDeleted(false);
				iisi.setIisc(iis.getCode());
				iisi.setNumber(item.getCount());
				result = iisiDAO.add(iisi);
				// 保存库存
				Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
				List<InvStorage> dbList = isDAO.findByCcDc(item.getCc(),item.getDc(), organDAO.getSonCodeList(cu_organCode(request)));
				String serial = isdDAO.findByCcOc(item.getCc(), organDAO.getSonCodeList(oc));
				if (null != dbList && dbList.size() > 0) {
					if (dbList.get(0).getCount().intValue() <= 0 ) {
						print(response, Msg.EXISTS);
						return;
					}
					dbList.get(0).setCount(dbList.get(0).getCount() + item.getCount());
					result = isDAO.upd(dbList.get(0));
					if (item.getSnc().intValue() == 3 || (item.getSnc().intValue() == 2 && serial == null)) {
						//保存退货串号
						for(InvReturnDetail ird : item.getDetailList()){
							if (ird != null && ird.getSerial() != null) {
								ird.setDeleted(false);
								ird.setRic(item.getCode());
								result = detailDAO.add(ird);	
								// 保存库存串号
								if (result == RESULT.SUCCESS){
									InvStorageDetail isd = new InvStorageDetail();
									isd.setDeleted(false);
									isd.setIsc(dbList.get(0).getCode());
									isd.setSerial(ird.getSerial());
									result = isdDAO.add(isd);								
								}
								//保存入库串号
								InvInStorageDetail iosd = new InvInStorageDetail();
								iosd.setDeleted(false);
								iosd.setIisic(iisi.getCode());
								iosd.setSerial(ird.getSerial());
								result = iisdDAO.add(iosd);
							}
						}
					}else if(item.getSnc().intValue() == 2 && serial != null) {
						//保存退货串号
						InvReturnDetail ird = new InvReturnDetail();
						ird.setDeleted(false);
						ird.setRic(item.getCode());
						ird.setSerial(serial);
						result = detailDAO.add(ird);	
						/*// 保存库存串号
						if (result == RESULT.SUCCESS){
							InvStorageDetail isd = new InvStorageDetail();
							isd.setDeleted(false);
							isd.setIsc(dbList.get(0).getCode());
							isd.setSerial(serial);
							result = isdDAO.add(isd);								
						}*/
						//保存出库串号
							InvInStorageDetail iosd = new InvInStorageDetail();
							iosd.setDeleted(false);
							iosd.setIisic(iisi.getCode());
							iosd.setSerial(serial);
							result = iisdDAO.add(iosd);
					}
				} else {
					InvStorage ins = new InvStorage();
					ins.setCc(item.getCc());
					ins.setCount(item.getCount());
					ins.setDc(item.getDc());
					ins.setOc(cu_organCode(request));
					ins.setPrice(item.getPrice());
					ins.setSnc(item.getSnc());
					if (item.getSnc() == 3 || (item.getSnc().intValue() == 2 && serial == null)){
						for(InvReturnDetail ird : item.getDetailList()){
							if (ird != null && ird.getSerial() != null) {
								// 保存退货详情
								ird.setDeleted(false);
								ird.setRic(item.getCode());
								result = iisdDAO.add(ird);									
								if (result == RESULT.SUCCESS){
									//	保存入库串号
									InvInStorageDetail iisd= new InvInStorageDetail();
									iisd.setDeleted(false);
									iisd.setIisic(iis.getCode());
									iisd.setSerial(ird.getSerial());
									iisdDAO.add(iisd);
									// 保存库存串号
									InvStorageDetail isd = new InvStorageDetail();	
									isd.setDeleted(false);
									isd.setIsc(ins.getCode());
									isd.setSerial(iisd.getSerial());
									result = detailDAO.add(isd);								
								}
							}
						}
					}
					if (item.getSnc().intValue() == 2 && serial != null) {
						InvReturnDetail ird = new InvReturnDetail();
						// 保存退货详情
						ird.setDeleted(false);
						ird.setRic(item.getCode());
						result = iisdDAO.add(ird);	
						// 保存入库序列号
						InvInStorageDetail iisd = new InvInStorageDetail();
						iisd.setDeleted(false);
						iisd.setIisic(item.getCode());
						iisd.setSerial(serial);
						result = iisdDAO.add(iisd);
						if (result == RESULT.SUCCESS){
							InvStorageDetail isd = new InvStorageDetail();	
							isd.setDeleted(false);
							isd.setIsc(ins.getCode());
							isd.setSerial(iisd.getSerial());
							result = detailDAO.add(isd);								// 保存库存串号
						}
					}
				}
			}
		}
		print(response, Msg.SUCCESS);
	}
  
	/**
	 * 查询库存商品
	 */
	@RequestMapping(Url.S_INIT)
	public ModelAndView sInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.INDEX, request.getParameter(Parm.INDEX));
		System.out.println(request.getParameter(Parm.INDEX));
		return new ModelAndView(Url.INVRETURNSALES_S_INIT);
	}
	
	@RequestMapping(Url.S_LIST)
	public ModelAndView sList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME,Parm.FAST);
		if (RESULT.SUCCESS == isDAO.findByMap(organDAO.getSonCodeList(cu_organCode(request)), map,pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVRETURNSALES_S_LIST);
	}
  
	/**
	 * 查询仓库
	 */
	@RequestMapping(Url.AID_INIT_1)
	public ModelAndView aidInit1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.INDEX, request.getParameter(Parm.INDEX));
		System.out.println(request.getParameter(Parm.INDEX));
		return new ModelAndView(Url.INVRETURNSALES_AID_INIT1);
	}
	
	@RequestMapping(Url.AID_LIST_1)
	public ModelAndView aidList1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME);
		if (RESULT.SUCCESS == idDAO.findByMap(organDAO.getSonCodeList(cu_organCode(request)), map,pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVRETURNSALES_AID_LIST1);
	}
	/**
	 * 查询供应商
	 */
	@RequestMapping(Url.AID_INIT_2)
	public ModelAndView aidInit2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.INVRETURNSALES_AID_INIT2);
	}
	
	@RequestMapping(Url.AID_LIST_2)
	public ModelAndView aidList2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.OC, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == ibDAO.findByMap(oc, map,pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.INVRETURNSALES_AID_LIST2);
	}
	//判断序列号是否是该库存下的商品所属
	@RequestMapping(value = Url.VERIFY2)
	public void serial2(HttpServletRequest request, HttpServletResponse response)throws Exception {
		String serial = request.getParameter("serial");
		String cc = request.getParameter("cc");
		String dc = request.getParameter("dc");
		List<InvStorageDetail> detailList = isdDAO.findBySerialCcDc(serial,Integer.parseInt(cc),Integer.parseInt(dc));
		if (detailList != null && detailList.size() > 0) {
			print(response, Msg.SUCCESS);
			
		} else {
			print(response, Msg.ERROR);
			return;
		}
	}
	
	// 用序列号到库存查询出对应的商品信息
	@RequestMapping(value = Url.VERIFY)
	public void serial(HttpServletRequest request, HttpServletResponse response){
		String serial = request.getParameter("serial");
		List<InvStorageDetail> detailList = isdDAO.findBySerial(serial);
		if (detailList != null && detailList.size() > 0) {
			print(response, Msg.ERROR);
		} else {
			print(response, Msg.SUCCESS);
		}
	}
	
	@RequestMapping(value = Url.CHECK)
	public void check(HttpServletRequest request, HttpServletResponse response){
		String cc = request.getParameter("cc");
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		String serial = isdDAO.findByCcOc(Integer.parseInt(cc), organDAO.getSonCodeList(oc));
		print(response, serial);
	}	
	
	@Autowired
	public void setInfoDAO(InvReturnInfoDAO infoDAO) {
		this.infoDAO = infoDAO;
	}
  	@Autowired
	public void setItemDAO(InvReturnItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
  	@Autowired
	public void setDetailDAO(InvReturnDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}
  	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
  	@Autowired
	public void setIsDAO(InvStorageDAO isDAO) {
		this.isDAO = isDAO;
	}
  	@Autowired
	public void setIsdDAO(InvStorageDetailDAO isdDAO) {
		this.isdDAO = isdDAO;
	}
  	@Autowired
	public void setIbDAO(InvBusinessDAO ibDAO) {
		this.ibDAO = ibDAO;
	}
  	@Autowired
	public void setIbcfDAO(InvBillConfingDAO ibcfDAO) {
		this.ibcfDAO = ibcfDAO;
	}
  	@Autowired
	public void setIdDAO(InvDepotDAO idDAO) {
		this.idDAO = idDAO;
	}
  	@Autowired
	public void setIisDAO(InvInStorageDAO iisDAO) {
		this.iisDAO = iisDAO;
	}
  	@Autowired
	public void setIisiDAO(InvInStorageItemDAO iisiDAO) {
		this.iisiDAO = iisiDAO;
	}
  	@Autowired
	public void setIisdDAO(InvInStorageDetailDAO iisdDAO) {
		this.iisdDAO = iisdDAO;
	}
  	
}
